package com.itheima.publisher.amqp;

import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/9/14 12:28
 * @注释
 */
@SpringBootTest
public class SpringAmqpTest {
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 向消息队列中直接发送一条消息
     */
    @Test
    public void testSimpleQueue(){
        //声明队列名称
        String queueName = "simple.queue";
        //构建消息
        String message = "hello simple.queue";
        //发送消息
        rabbitTemplate.convertAndSend(queueName, message);
    }

    /**
     * 向队列中不断发送消息，模拟消息堆积
     */
    @Test
    public void testWorkQueue() throws InterruptedException {
        //队列名称
        String queueName = "work.queue";
        //消息
        String message = "hello message_";
        for (int i=0;i<50;i++){
            rabbitTemplate.convertAndSend(queueName, message + i);
            Thread.sleep(20);
        }
    }

    /**
     * fanout交换机
     */
    @Test
    public void testFanoutExchange(){
        //交换机名称
        String exchangeName = "moresuo.fanout";
        //消息
        String msg = "hello,everyone";
        rabbitTemplate.convertAndSend(exchangeName, "", msg);
    }

    @Test
    public void testDirectExchange(){
        //交换机名称
        String exchangeName = "moresuo.direct";
        //消息
        String msg = "爱江山打卡机上党课";
        rabbitTemplate.convertAndSend(exchangeName, "blue", msg);

    }

    @Test
    public void testTopicExchange(){
        //交换机名称
        String exchangeName = "moresuo.topic";
        //消息
        String msg = "啊就是快递哈科技";
        rabbitTemplate.convertAndSend(exchangeName, "japan.news", msg);
    }

    @Test
    public void testSendMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "柳岩");
        map.put("age", 21);
        //发送消息
        rabbitTemplate.convertAndSend("object.queue", map);
    }
}
